package com.example.service;

import com.example.dao.UserMapper;
import com.example.domain.t_user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class IUserService {

    @Autowired
    private UserMapper user;

    //查询用户根据（ID，username，Email）
    public t_user Select(int id){
        return user.selectById(id);
    }
    public t_user SelectEmail(String email){
        return user.selectByEmail(email);
    }
    public t_user Select(String username){
        return user.selectByUsername(username);
    }
    //查询用户权限
    public String SelectAuthority(int id){
        return user.selectUserAuthority(id);
    }
    //判断用户权限是否为root
    public boolean Isroot(int id){
        return user.selectUserAuthority(id).contains("root");
    }
    //判断用户权限是否为root
    public boolean Isroot(t_user us){
        if(us==null)return false;
        return user.selectUserAuthority(us.getId()).contains("root");
    }

    /**
     * 保存用户信息，将user插入数据库中
     * @param u
     * @return
     */
    public boolean insert(t_user u){
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(u.getPassword());
        u.setPassword(encode);
        //System.out.println(u);
        user.insertOneUser(u);
        u = user.selectByUsername(u.getUsername());
        return user.insertOneAuth(u.getId());
    }

    //根据email修改用户激活标识
    public void setVail(String email,boolean valid) {
        user.setUserVail(email,valid);
    }

    //根据用户id修改用户信息
    public boolean update(t_user loginUser) {
        return user.updateUserById(loginUser);
    }

    public void updateAvatar(String s, int id) {
        user.updateAvatarById(s,id);
    }

    //查找用户头像
    public String getAvatar(int id) {
        return user.getAvatarById(id);
    }
    public String getAvatar(String author) {
        return user.getAvatarByAuthor(author);
    }
}
